package com.keffect.effects.animatedEffects;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Rectangle;

import com.keffect.effects.AnimatedEffect;

/**
 * 
 * @author Matias Varela
 *         <p>
 *         Student of Licenciatura en Ciencias de la Computacion, at the
 *         Universidad Nacional del Sur (Argentina).
 *         <p>
 *         project site: http://code.google.com/p/keffect-project/
 * 
 */
public class SplitHorizontalFade extends AnimatedEffect {

	private Rectangle r1, r2;
	private Paint defaultPaint;
	private boolean isInit;
	private Color color;


	public SplitHorizontalFade(long duration) {
		super(duration);
		isInit = false;
		color = Color.black;
	}


	public SplitHorizontalFade(long duration, Color color) {
		super(duration);
		isInit = false;
		this.color = color;
	}


	@Override
	public void initEffectProperties() {
		if (!isInit) {
			int width = (int) getEffectPanel().getWidth();
			int height = (int) getEffectPanel().getHeight();
			this.setFrames((long) height / 2);
			r1 = new Rectangle(0, 0, width, height / 2);
			r2 = new Rectangle(0, height / 2, width, height / 2);
			isInit = true;
		}
		r1.setLocation(0, 0);
		r2.setLocation(0, (int) r2.getHeight());
	}


	@Override
	public boolean isRunning() {
		return (r1.getY() + r1.getHeight()) > 0;
	}


	@Override
	public void run() {
		r1.translate(0, -1);
		r2.translate(0, 1);
	}


	@Override
	public void doPaintBefore(Graphics2D g) {
	}


	@Override
	public void doPaintAfter(Graphics2D g) {
		defaultPaint = g.getPaint();
		g.setPaint(getNewPaint1());
		g.fill(r1);
		g.setPaint(this.getNewPaint2());
		g.fill(r2);
		g.setPaint(defaultPaint);
	}


	private GradientPaint getNewPaint2() {
		return new GradientPaint(new Point((int) r2.getX(), (int) r2.getY()), new Color(0.0f, 0.0f, 0.0f, 0.0f), new Point((int) r2.getX(),
				(int) (r2.getY() + r2.getHeight() * 0.15)), color);
	}


	private GradientPaint getNewPaint1() {
		return new GradientPaint(new Point((int) r1.getX(), (int) (r1.getY() + r1.getHeight() * 0.85)), color, new Point((int) r1.getX(),
				(int) (r1.getHeight() + r1.getY())), new Color(0.0f, 0.0f, 0.0f, 0.0f));
	}
}
